<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       Cake.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('Controller', 'Controller');

/**
 * This is a placeholder class.
 * Create the same file in app/Controller/AppController.php
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       Cake.Controller
 * @link http://book.cakephp.org/view/957/The-App-Controller
 */
class AppController extends Controller {

  function  beforeFilter() {
    parent::beforeFilter();
    $prefix = isset($this->request->params['prefix']) ? $this->request->params['prefix'] : null;
    //Perform Security Validation here
    //Authentication
    
    if(!empty($prefix)) {
      $user= $this->Session->read('User');
      if(empty($user)) {
        //store intended url
        $this->Session->write('destinationUrl',$this->request->url);
        //redirect user
        $this->redirect('/login');
      } else {
        //check roles
        if( ($prefix == Configure::read('App.adminRole') && !in_array(Configure::read('App.adminRole'), $user['UserRoles']))  ) {
          $this->Session->setFlash("You do not have sufficient privileges to access this resource");
          $this->redirect('/contents/message');
        }
      }
    }
    $valid = true;
  }

  function _processFieldName($fieldNameIn) {
    $retVal = "";
    if (!empty($fieldNameIn)) {
      if(strpos($fieldNameIn,"_")===0) { // field names that start with underscores _ are hidden and will be returned
        $retVal = $fieldNameIn;
      } else {
        $result = preg_split('/([[:upper:]][[:lower:]]+)/', $fieldNameIn, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
        array_shift($result);
        $retVal = strtolower(implode("_", $result));
      }
    }
    return $retVal;
  }

  function _processRequestData($request) {
    $retVal = null;
    if (!empty($request)) {
      //formatting returned object
      $dataIn = array();
      foreach ($request->data as $key => $value) {
        $dataIn[$this->modelClass][$this->_processFieldName($key)] = $value;
      }
      //Getting file inputs
      $dataInputs = array();
      if (isset ($request->params) && isset($request->params['form'])){
        foreach ($request->params['form'] as $key => $value) {
          $dataIn[$this->modelClass][$this->_processFieldName($key)] = $value;
        }
      }
        $retVal = array_merge($dataIn, $dataInputs);      
    }
    return $retVal;
  }
}
